const { reactive, toRefs } = Vue;

export default function useReactive(initialState) {
  const state = reactive(initialState); //reactive包装
  const stateRefs = toRefs(state); //解包操作
  const setState = (key, value) => {
    if ({}.toString.call(key) === '[object Object]') {
      for (let k in key) {
        if (initialState.hasOwnProperty(k)) {
          state[k] = key[k];
        }
      }
    } else {
      //非对象
      if (typeof value === 'function') {
        state[key] = value(state[key]);
      } else {
        state[key] = value;
      }
    }
  };
  return [state, stateRefs, setState];
}
